//#include <stdio.h>
#include <string>

#define CRC32_POLY 0xEDB88320 // IEEE 802.3 
//#define CRC32_POLY 0x82F63B78 // Castagnoli

unsigned int crc_table[256];
void make_crc_table(){ unsigned int i, j, x;
for (i=0; i<256; i++){ x = i;
for (j=8; j>0; j--)if(x&1) x=(x>>1)^CRC32_POLY; else x>>=1;
crc_table[i]=x; }}


unsigned int crc32(const char* p , unsigned int length){
static int crc_init=0; unsigned int i, x = 0xFFFFFFFF;
if(!crc_init) { crc_init=1; make_crc_table(); }
for (i = 0; i < length; i++) x = ((x >> 8) & 0x00FFFFFF) ^ crc_table[(x ^ p[i]) & 0xFF];
return x ^ 0xFFFFFFFF; }


unsigned int crc32( const std::string &p) { return crc32( &p[0], p.size() ); } 
